/**
 * 
 */
package edu.gtcfla.km.security.realm;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.subject.MutablePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.vsg.biz.account.Role;
import org.vsg.biz.account.repositories.PermissionRepo;
import org.vsg.security.DefaultResourceslPermission;
import org.vsg.security.ResourcesPermission;
import org.vsg.security.ResourcesPermissionException;
import org.vsg.security.strategy.MenuPermissionRule;

import edu.gtcfla.km.security.RoleStrategyRule;

/**
 * @author vison
 *
 */
public class SimpleMenuPermissionRule implements MenuPermissionRule {

	@Autowired(required=false)		
	private PermissionRepo<org.vsg.biz.account.Permission , String> permissionRepo;

	@Autowired(required=false)		
	private RoleStrategyRule roleStrategyRule;
	
	
	/* (non-Javadoc)
	 * @see org.vsg.security.strategy.PermissionRule#process()
	 */
	@Override
	public Map<String ,Permission> process() {
		// TODO Auto-generated method stub
		Collection<Permission> priColl = principalCollection.fromRealm(MenuPermissionRule.ACCOUNT_ACTIVE_PERM);
		
		// --- load all role permisson ---
		if (priColl == null || priColl.isEmpty()) {
			// --- find all role from cache ---
			Set<Role> allRole =  roleStrategyRule.findActiveRoles((MutablePrincipalCollection)principalCollection );
			
			Set<String> allRoleIds = new LinkedHashSet<String>();
			
			Iterator<Role> activeRoleIter = allRole.iterator();
			
			while (activeRoleIter.hasNext()) {
				allRoleIds.add( activeRoleIter.next().getId() );
			}

			
			Set<org.vsg.biz.account.Permission> perms =  permissionRepo.findInMenu( allRoleIds );

			// ---- convert system permission ---
			try {
				
				priColl = new LinkedHashSet<Permission>();
				
				for (org.vsg.biz.account.Permission perm : perms) {
					String resId = perm.getAssignedRecourceId();
					String permResId =  ResourcesPermission.Helper.parseResourceId( resId );
					String permShortStr = "mnu:"+ perm.getAccessControl() +":"+permResId;
					DefaultResourceslPermission drp = new DefaultResourceslPermission(permShortStr);
					
					priColl.add( drp );
				}
				
				if (!priColl.isEmpty()) {
					
					principalCollection.addAll(priColl, MenuPermissionRule.ACCOUNT_ACTIVE_PERM);
					
				}

			} catch (ResourcesPermissionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		Map<String,Permission> resPerm = new LinkedHashMap<String,Permission>();
		
		for (Permission perm : priColl) {
			ResourcesPermission resperm = (ResourcesPermission)perm;
			String respath = resperm.getFullpathResource();
			
			String  resourceId = ResourcesPermission.Helper.recovertResourceId( respath );
			resPerm.put( resourceId , resperm);
		}

		return resPerm;
	}

	private MutablePrincipalCollection principalCollection;

	/* (non-Javadoc)
	 * @see org.vsg.security.strategy.MenuPermissionRule#setSubjectPrincipalCollection(org.apache.shiro.subject.PrincipalCollection)
	 */
	@Override
	public void setSubjectPrincipalCollection(
			MutablePrincipalCollection principalCollection) {
		// TODO Auto-generated method stub
		this.principalCollection = principalCollection;
		
		
	}


}
